#include "widget.h"
#include "ui_widget.h"

#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 固定登录窗口大小
    this->setFixedSize(400,300);
    myMainWindow = new MyMainWindow; // 创建主页面
    regist = new Registration; // 创建注册页面
    // 设置左上角图标
    this->setWindowIcon(QIcon(":/images/res/BitMap/key2.bmp"));
    // 监听注册页面返回信息
    connect(regist, &Registration::registToLogin, this, &Widget::registToLogin);
    // 监听操作事件
    connect(this, &Widget::actionOpt, myMainWindow, &MyMainWindow::widgetToEdit);
    // 设置密码框
    ui->pwdLine->setEchoMode(QLineEdit::Password);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_loginBtn_clicked()
{

    // 用户点击登录校验用户数据
    QString name = ui->nameLine->text();
    QString pwd = ui->pwdLine->text();
    if(regist->isUserPwd(name, pwd))
    {
        emit actionOpt(name + "登录了");
        this->hide();
        myMainWindow->show();
    }
    else
        QMessageBox::critical(this, "错误", "用户名或密码错误");
}

void Widget::on_registerBtn_clicked()
{
    this->hide();
    regist->show();
}

void Widget::registToLogin()
{
    regist->hide();
    this->show();
}

void Widget::on_quitBtn_clicked()
{
    exit(0);
}

void Widget::on_clearBtn_clicked()
{
    ui->nameLine->setText("");
    ui->pwdLine->setText("");
}
